import axios from "axios";

//封装axios
//创建axios实例
const service = axios.create({
    baseURL: "https://www.fastmock.site/mock/bf1fcb3c2e2945669c2c8d0ecb8009b8/api",
    timeout: 5000,
    headers: {
        "Content-type" : "application/json;charset=utf-8"
    }
})

//请求拦截
service.interceptors.request.use((config) => {
    config.headers = config.headers || {}
    if(localStorage.getItem("token")){
        config.headers.token = localStorage.getItem("token") || ""
    }
    return config
})

//响应拦截
service.interceptors.response.use((res) => {
    //code为200则为成功
    console.log(location.href)
    //console.log(location.href==='http://localhost:8082/login')
    if(location.href=='http://localhost:8080/login'){
    
    // if(location.href.indexOf('login')){
       
        const code : number = res.data.data.code
        console.log(res.data.code)
        if(code != 200){
            return Promise.reject(res.data)
        }
        return res.data
    }
    else{
        const code : number = res.data.code
        console.log(res.data.code)
        if(code != 200){
            return Promise.reject(res.data)
        }
        return res.data
    }
    console.log(res.data)

    // const code : number = res.data.code
    // console.log(res.data.code)
    // if(code != 200){
    //     return Promise.reject(res.data)
    // }
    // return res.data
},(err) => {
    console.log(err)
})

export default service